
#include "ftcontrols.h"
#include <QApplication>
#include "configuration.h"

#define DEFAULT_FONT_PIXEL_SIZE 20
#define TITLE_FONT_PIXEL_SIZE 35

#define DEFAULT_HEIGHT 48
#define DEFAULT_PADDING 3
#define ICON_SIZE QSize(DEFAULT_HEIGHT, DEFAULT_HEIGHT)

#define SHAKY_THREESHOLD_PERC 8

FTControls* FTControls::myself = NULL;

FTControls::FTControls()
{
    screen_size = configuration->screenSize();
    QSize default_size = configuration->defaultScreenSize();

    shaky_threeshold = qMin( screen_size.width(), screen_size.height() ) * SHAKY_THREESHOLD_PERC / 100;

    factor = qMax( default_size.width() / screen_size.width(),
                          default_size.height() / screen_size.height() );

    // Ignore when screen is bigger than default (its ok... we are lucky!)
    if ( factor < 1.0 )
        factor = 1.0;

    // Fix QApplication default font size:
    fontNormal = QApplication::font();
    fontNormal.setPixelSize( DEFAULT_FONT_PIXEL_SIZE / factor );
#ifdef Q_OS_ANDROID
    fontNormal.setItalic( true );
#endif

    fontTitle = QApplication::font();
    fontTitle.setPixelSize( TITLE_FONT_PIXEL_SIZE / factor );
#ifdef Q_OS_ANDROID
    fontTitle.setItalic( true );
#endif

    fontSmall = QApplication::font();
    fontSmall.setPixelSize( DEFAULT_FONT_PIXEL_SIZE / factor / 1.2 );
#ifdef Q_OS_ANDROID
    fontSmall.setItalic( true );
#endif

     QApplication::setFont( fontNormal );

    myself = this;
}

bool FTControls::showAds()
{
    return configuration->showAds();
}

void FTControls::initialize()
{
    if ( myself == NULL )
        myself = new FTControls();
}

QFont FTControls::font(FTControls::FontTypes type)
{
    switch ( type )
    {
    case FTControls::FontNormal:
        return myself->fontNormal;
        break;

    case FTControls::FontTitle:
        return myself->fontTitle;
        break;

    case FTControls::FontSmall:
        return myself->fontSmall;
        break;

    default:
        return QFont();
    }
}

int FTControls::shakyThreeshold()
{
    return myself->shaky_threeshold;
}

QSize FTControls::iconSize(FTControls::IconSize size )
{
    switch ( size )
    {
    case FTControls::IconNormal:
        return ICON_SIZE / myself->factor;
        break;

    case FTControls::IconBig:
        return ICON_SIZE * 1.5 / myself->factor;
        break;

    case FTControls::IconSmall:
        return ICON_SIZE / 1.5 / myself->factor;
        break;

    default:
        return QSize();
    }
}

int FTControls::defaultHeight()
{
    return DEFAULT_HEIGHT / myself->factor;
}

int FTControls::defaultPadding()
{
    return DEFAULT_PADDING;
}

QSize FTControls::adjustSize(const QSize &s )
{
    return s / myself->factor;
}
